﻿' LibWowArmory
' by Ronald M. Clifford (roncli@roncli.com)
'
' This source code is released under the GNU Library General Public License (LGPL) Version 2.1.

Imports System
Imports System.Collections.ObjectModel
Imports System.Globalization
Imports System.IO
Imports System.Linq
Imports System.Xml.Serialization

Namespace roncliProductions.LibWowArmory.Item.Search

    ''' <summary>
    ''' A class to search items on the Armory with
    ''' </summary>
    ''' <remarks></remarks>
    Public Class ItemSearch
        Inherits WowArmoryData

#Region "Declarations"

        Private issPage As New Schema.page

#End Region

#Region "Properties"

        ''' <summary>
        ''' The current filter to query against
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property Filter As QueryFilter

        ''' <summary>
        ''' The URL that will be used to search items on the Armory with
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Overrides ReadOnly Property URL As String
            Get
                Dim ubURL As New URLQueryStringCollection("http://www.wowarmory.com/search.xml")

                ubURL.Add("searchType", "items")

                'Search query
                If Not String.IsNullOrEmpty(Filter.SearchQuery) Then
                    ubURL.Add("searchQuery", Filter.SearchQuery)
                End If

                'Source filters
                If Not String.IsNullOrEmpty(Filter.Source.Source) Then
                    ubURL.Add("fl[source]", Filter.Source.Source)
                End If
                If Not String.IsNullOrEmpty(Filter.Source.Dungeon) Then
                    ubURL.Add("fl[dungeon]", Filter.Source.Dungeon)
                End If
                If Filter.Source.Boss <> 0 Then
                    ubURL.Add("fl[boss]", Filter.Source.Boss.ToString(CultureInfo.InvariantCulture))
                End If
                If Not String.IsNullOrEmpty(Filter.Source.Difficulty) Then
                    ubURL.Add("fl[difficulty]", Filter.Source.Difficulty)
                End If
                If Not String.IsNullOrEmpty(Filter.Source.Faction) Then
                    ubURL.Add("fl[faction]", Filter.Source.Faction)
                End If
                If Not String.IsNullOrEmpty(Filter.Source.Pvp) Then
                    ubURL.Add("fl[pvp]", Filter.Source.Pvp)
                End If

                'Item Type filters
                If Not String.IsNullOrEmpty(Filter.ItemType.ItemType) Then
                    ubURL.Add("fl[type]", Filter.ItemType.ItemType)
                End If
                If Filter.ItemType.DesiredBy <> 0 Then
                    ubURL.Add("fl[usbleBy]", Filter.ItemType.DesiredBy.ToString(CultureInfo.InvariantCulture))
                End If
                If Not String.IsNullOrEmpty(Filter.ItemType.DesiredByRole) Then
                    ubURL.Add("fl[classType]", Filter.ItemType.DesiredByRole)
                End If
                If Not String.IsNullOrEmpty(Filter.ItemType.Rarity) Then
                    ubURL.Add("fl[rrt]", Filter.ItemType.Rarity)
                End If
                If Not String.IsNullOrEmpty(Filter.ItemType.WeaponSlot) Then
                    ubURL.Add("fl[slot]", Filter.ItemType.WeaponSlot)
                End If
                If Not String.IsNullOrEmpty(Filter.ItemType.WeaponType) Then
                    ubURL.Add("fl[subTp]", Filter.ItemType.WeaponType)
                End If
                If Filter.ItemType.RequiredLevelMin <> 0 Then
                    ubURL.Add("fl[rqrMin]", Filter.ItemType.RequiredLevelMin.ToString(CultureInfo.InvariantCulture))
                End If
                If Filter.ItemType.RequiredLevelMax <> 0 Then
                    ubURL.Add("fl[rqrMax]", Filter.ItemType.RequiredLevelMax.ToString(CultureInfo.InvariantCulture))
                End If
                If Not String.IsNullOrEmpty(Filter.ItemType.ArmorSlot) Then
                    ubURL.Add("fl[slot]", Filter.ItemType.ArmorSlot)
                End If
                If Not String.IsNullOrEmpty(Filter.ItemType.ArmorType) Then
                    ubURL.Add("fl[subTp]", Filter.ItemType.ArmorType)
                End If
                If Not String.IsNullOrEmpty(Filter.ItemType.GemType) Then
                    ubURL.Add("fl[subTp]", Filter.ItemType.GemType)
                End If
                If Not String.IsNullOrEmpty(Filter.ItemType.BagType) Then
                    ubURL.Add("fl[subTp]", Filter.ItemType.BagType)
                End If
                If Not String.IsNullOrEmpty(Filter.ItemType.ProjectilesType) Then
                    ubURL.Add("fl[subTp]", Filter.ItemType.ProjectilesType)
                End If
                If Not String.IsNullOrEmpty(Filter.ItemType.QuiversType) Then
                    ubURL.Add("fl[subTp]", Filter.ItemType.QuiversType)
                End If
                If Not String.IsNullOrEmpty(Filter.ItemType.RecipesType) Then
                    ubURL.Add("fl[subTp]", Filter.ItemType.RecipesType)
                End If
                If Filter.ItemType.SkillLevelMin <> 0 Then
                    ubURL.Add("fl[minSkill]", Filter.ItemType.SkillLevelMin.ToString(CultureInfo.InvariantCulture))
                End If
                If Filter.ItemType.SkillLevelMax <> 0 Then
                    ubURL.Add("fl[maxSkill]", Filter.ItemType.SkillLevelMax.ToString(CultureInfo.InvariantCulture))
                End If
                If Not String.IsNullOrEmpty(Filter.ItemType.PermanentEnchantmentSlot) Then
                    ubURL.Add("fl[slot]", Filter.ItemType.PermanentEnchantmentSlot)
                End If

                For Each aoOption As AdvancedOptionsFilter In Filter.AdvancedOptions
                    If aoOption.AdvancedOptionsFilter < Enums.AdvancedOptionsType.General_BindOnPickup Then
                        ubURL.Add("fl[advOpt]", String.Format(CultureInfo.InvariantCulture, "{0}_{1}_{2}", aoOption.AdvancedOptions, aoOption.Operator, aoOption.Range))
                    Else
                        ubURL.Add("fl[advOpt]", aoOption.AdvancedOptions)
                    End If
                Next

                Return ubURL.ToString
            End Get
        End Property

        ''' <summary>
        ''' Indicates to the inherited class that this request does not require login
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Overrides ReadOnly Property RequiresLogin As Boolean
            Get
                Return False
            End Get
        End Property

        ''' <summary>
        ''' Returns a list of searched items
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property Items As Collection(Of Item)
            Get
                If issPage Is Nothing Then
                    Return New Collection(Of Item)
                End If
                If issPage.armorySearch(0).searchResults(0).items(0).item Is Nothing Then
                    Return New Collection(Of Item)
                End If
                Return (From a In issPage.armorySearch(0).searchResults(0).items(0).item Select New Item(a.icon, a.id, a.name, CType(a.rarity, Enums.RarityType), a.url, Integer.Parse(a.filter.Where(Function(f) f.name = "relevance").First.value, CultureInfo.InvariantCulture))).ToCollection
            End Get
        End Property

#End Region

#Region "Methods"

        ''' <summary>
        ''' Performs the search on the armory
        ''' </summary>
        ''' <remarks></remarks>
        Public Sub Search()
            MyBase.Retrieve()

            Dim xsSerializer As New XmlSerializer(issPage.GetType)

            Using srXML As New StringReader(MyBase.Data)
                issPage = CType(xsSerializer.Deserialize(srXML), Schema.page)
            End Using
        End Sub

#End Region

    End Class

End Namespace
